package src;

import javazoom.jl.player.Player;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;


public class UI extends JFrame implements KeyListener {
    WarPanel warPanel=new WarPanel();
    Player player;


    public UI(){
        this.setTitle("赤色妖精花2.0");
        this.setLocation(0,0);
        this.setSize(1024,680);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.add(warPanel);
        this.addKeyListener(this);
        warPanel.action();

    }

    public static void main(String[] args) {
        UI ui=new UI();

        ui.init();

    }
    public void init(){
        while(true)
        {


            try
            {
               InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("music/bgm12.mp3");
//
//
             playmusic(resourceAsStream);


                Thread.sleep(82800);
            }catch (Exception e)
            {
                e.printStackTrace();
            }

        }

    }
    public void playmusic(InputStream inputStream) throws Exception{
//        BufferedInputStream bufferedInputStream=new BufferedInputStream(inputStream);
        player=new Player(inputStream);
        player.play();


    }

    public void playmusic(String music) throws Exception{
        BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream(music));
        player=new Player(bufferedInputStream);
        player.play();
    }

    public void sound(String str)
    {
        try {
            FileInputStream in = new FileInputStream(str);
            AudioStream as=new
                    AudioStream(in);
            AudioPlayer.player.start(as);
        }catch(Exception e)
        {
            e.printStackTrace();
        }

    }

    public void sound(InputStream inputStream)
    {
        try {


            AudioStream as=new
                    AudioStream(inputStream);
            AudioPlayer.player.start(as);
        }catch(Exception e)
        {
            e.printStackTrace();
        }

    }

























    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
//        System.out.println(e.getKeyCode());
        warPanel.restart(e.getKeyCode());
        warPanel.manmove(e.getKeyCode());
        warPanel.mastermove(e.getKeyCode());

    }

    @Override
    public void keyReleased(KeyEvent e) {
        warPanel.mastercancel(e.getKeyCode());
        warPanel.mancancel(e.getKeyCode());

    }
}
